<template>
	<view>
		<uni-collapse>
			<uni-collapse-item title="修改资料">
				<view class="uni-flex uni-row">
					<view class="uni-form-item" style="width: 300rpx;">
						<view class="title">新密码</view>
					</view>
					<view class="uni-form-item" style="margin-right: 30rpx;">
						<input password="true" class="uni-input" ref="edit_password" />
					</view>
				</view>
				<view class="uni-flex uni-row">
					<view class="uni-form-item" style="width: 300rpx;">
						<view class="title">名称</view>
					</view>
					<view class="uni-form-item" style="margin-right: 30rpx;">
						<input class="uni-input" ref="edit_name" />
					</view>
				</view>
				<view class="uni-form-item">
					<button class="uni-btn" type="primary" @click="onSetPasswordSubmit" style="font-size: 18rpx; width: 300rpx; margin: 30rpx auto;">修改</button>
				</view>
			</uni-collapse-item>
		</uni-collapse>
		<uni-collapse v-if="is_admin">
			<uni-collapse-item title="添加用户">
				<view class="uni-flex uni-row">
					<view class="uni-form-item">
						<view class="title">账户</view>
						<input class="uni-input" ref="username" />
					</view>
				</view>
				<view class="uni-flex uni-row">
					<view class="uni-form-item">
						<view class="title">名称</view>
						<input class="uni-input" ref="name" />
					</view>
				</view>
				<view class="uni-flex uni-row">
					<view class="uni-form-item">
						<view class="title">密码</view>
						<input password="true" class="uni-input" ref="password" />
					</view>
				</view>
				<button type="primary" @click="onSaveUser">添加</button>
			</uni-collapse-item>
		</uni-collapse>
		<uni-collapse v-if="is_admin" @change="getUserList">
			<uni-collapse-item title="管理用户" name="user_list">
				<uni-list>
					<template v-for="(item, key) in user_list">
						<uni-list-item :clickable="true" @click="onUserItemClick(key)" :title="item.user_id+' '+item.username+' ' +item.name+' '+item.status"></uni-list-item>
					</template>
				</uni-list>
			</uni-collapse-item>
		</uni-collapse>
		<uni-list>
			<uni-list-item :clickable="true" title="退出登录" @click="logout" :rightText="name" ></uni-list-item>
		</uni-list>
	</view>
</template>

<script>
	import uniList from '@/components/uni-list/uni-list.vue'
	import uniListItem from '@/components/uni-list-item/uni-list-item.vue'
	import uniCollapse from '@/components/uni-collapse/uni-collapse.vue'
	import uniCollapseItem from '@/components/uni-collapse-item/uni-collapse-item.vue'
	export default {
		components: {
			uniCollapse,
			uniCollapseItem,
			uniList,
			uniListItem
		},
		data() {
			return {
				token: "",
				user_list: [],
				is_admin: 0,
				name:""
			}
		},
		onShow() {
			var token_obj = this.common.checkNeedLogin()
			if (token_obj != null) {
				this.token = token_obj.token
				this.is_admin = token_obj.is_admin
				this.name = token_obj.name
			}
		},
		methods: {
			onUserItemClick(index) {
				var _this = this
				var obj = _this.user_list[index]
				uni.showActionSheet({
					itemList: ['修改密码', '停用用户', '恢复用户'],
					success: function(res) {
						if (res.tapIndex == 0) {
							var new_password = window.prompt("请输入新密码", "")
							if (new_password != null) {
								new_password = new_password.replace(/[^\w\.\/]/ig, '')
								if (new_password.length < 6 || new_password.length > 16) {
									uni.showToast({
										icon: 'none',
										title: '密码只能字母或数字，长度6-16'
									});
									return false;
								}
							}

							var post_data = {
								token: _this.token,
								user_id: obj.user_id,
								password: new_password
							}
							_this.common.requestAPI(_this.common.config.API_UPDATE_USER, 'POST', post_data, '正在修改', function(data) {
								uni.showToast({
									title: '修改成功'
								})
							})
						} else if (res.tapIndex == 1) {
							uni.showModal({
								title: '提示',
								content: '确定删除该用户吗',
								success: function(res) {
									_this.deleteUser(obj.user_id)
								},
							})
						} else if (res.tapIndex == 2) {
							_this.common.requestAPI(_this.common.config.API_UPDATE_USER, 'POST', {
								token: _this.token,
								user_id: obj.user_id,
								status: 'NORMAL'
							}, '正在恢复', function(data) {
								uni.showToast({
									title: '恢复成功'
								})
							})
						}
					}
				});
			},
			deleteUser(user_id) {
				var post_data = {
					user_id: user_id,
					token: this.token
				}
				var _this = this
				this.common.requestAPI(this.common.config.API_DELETE_USER, 'POST', post_data, '正在删除', function(data) {
					uni.showToast({
						title: '删除成功'
					})
					_this.getUserList(['user_list'])
				})
			},
			getUserList(res) {
				if (res.length > 0) {
					var _this = this
					var post_data = {
						token: this.token
					}
					this.common.requestAPI(this.common.config.API_GET_ALL_USER, 'GET', post_data, null, function(data) {
						_this.user_list = data.result
					})
				}
			},
			onSaveUser() {
				var name = this.$refs.name.valueSync;
				var username = this.$refs.username.valueSync;
				var password = this.$refs.password.valueSync;
				if (username.length < 3 || username.length > 16) {
					uni.showToast({
						icon: 'none',
						title: '账户只能字母或数字，长度3-16'
					});
					return false;
				}
				if (name.length < 1 || name.length > 16) {
					uni.showToast({
						icon: 'none',
						title: '名称长度1-16'
					});
					return false;
				}
				if (password.length < 6 || password.length > 16) {
					uni.showToast({
						icon: 'none',
						title: '密码只能字母或数字，长度6-16'
					});
					return false;
				}
				var post_data = {
					token: this.token,
					name: name,
					username: username,
					password: password
				}
				this.common.requestAPI(this.common.config.API_ADD_USER, 'POST', post_data, '正在保存', function(data) {
					uni.showToast({
						title: '已添加'
					})
				})
			},
			onSetPasswordSubmit() {
				var password = this.$refs.edit_password.valueSync;
				var name = this.$refs.edit_name.valueSync;
				var _this = this
				uni.showModal({
					title: '提示',
					content: '确定修改?',
					success: function(res) {
						var post_data = {
							name: name,
							token: _this.token,
							password: password
						}
						_this.common.requestAPI(_this.common.config.API_UPDATE_MY_INFO, 'POST', post_data, '正在修改', function(data) {
							uni.showToast({
								title: '修改成功'
							})
						})
					}
				})
			},
			logout() {
				this.common.logout()
			}
		}
	}
</script>

<style>

</style>
